<template>
  <div>
    <span class="txt11">{{date2}}</span>
    <div class="layer15 flex-col">
      <div class="mod8 flex-col justify-between">
        <span class="word26">{{date}}</span>
        <span class="word27">{{week}}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: () => ({
    timer: null,
    date: '',
    date2: '',
    week: ''
  }),
  created () {
    this.tick()
    this.setTime()
    this.getWeekDate()
  },
  methods: {
    setTime () {
      this.timer = setInterval(() => this.tick(), 1000)
    },
    tick () {
      const da = new Date()
      this.date = da.getFullYear() + '.' + (da.getMonth() + 1) + '.' + da.getDate()
      this.date2 = this.dateFormatHandler(da, 'hh:mm:ss')
    },
    /**
     * 日期格式化
     */
     dateFormatHandler (date, format) {
       if (!date) return ''
       date = new Date(date)
       format = format || 'yyyy-MM-dd hh:mm'
       var o = {
         'M+': date.getMonth() + 1, // 月份
         'd+': date.getDate(), // 日
         'h+': date.getHours(), // 小时
         'm+': date.getMinutes(), // 分
         's+': date.getSeconds(), // 秒
         'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
         'S': date.getMilliseconds() // 毫秒
       }
       if (/(y+)/.test(format)) {
         format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
       }
       for (var k in o) {
         if (new RegExp('(' + k + ')').test(format)) {
           format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
         }
       }
       return format
    },
    /**
  	 *获取当前星期几
  	 *
  	*/
  	getWeekDate() {
      var now = new Date();
      var day = now.getDay();
      var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
      this.week = weeks[day];
  	}
  },
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>
